﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BirthdayRemainder
{
    public partial class FrmModify : Form
    {
        public string ModifyName{get;set;}
        public FrmModify(string name)
        {
            InitializeComponent();
            ModifyName = name;
        }
        /// <summary>
        /// 重绘窗体，画一条直线
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Point p1 = new Point(0, this.btnModify.Location.Y - 10);
            Size size = new Size(this.Width, 2);
            Rectangle rect = new Rectangle(p1, size);
            e.Graphics.FillRectangle((Brush)Brushes.DarkGray, rect);
        }
        private void FrmModify_Load(object sender, EventArgs e)
        {
            PersonInfo info = SftMgr.DbGetRecord(ModifyName);
            this.tbName.Text = info.Name;
            this.dtpBirthday.Value = Convert.ToDateTime(info.Birthday);
            this.dtpBirthday.MaxDate = DateTime.Today.AddDays(-1);
            this.cbbSolorLunar.Text = info.SolorOrLunar;
            this.tbPhne.Text = info.Phone;
            this.Icon = BirthdayRemainder.Properties.Resources.ico_main;
        }
        private delegate DialogResult dl_Msg(string str, MessageBoxButtons btn);
        private DialogResult Msg(string str, MessageBoxButtons btn = MessageBoxButtons.OK)
        {
            if (this.InvokeRequired)
            {
                dl_Msg method = Msg;
                return (DialogResult)Invoke(method, str, btn);
            }
            return MessageBox.Show(str, "提醒", btn);
        }
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (!ValidateCheck())
            {
                return;
            }
            //如果修改了名称，则将原有的记录删除
            if (ModifyName != this.tbName.Text)
            {
                SftMgr.DbDeleteName(ModifyName);
            }
            PersonInfo info = new PersonInfo();
            info.Name = tbName.Text;
            info.Birthday = dtpBirthday.Value.ToString(GlobalValue.STR_DATE_TIME_FORMAT);
            info.IsLunar = cbbSolorLunar.Text == "阴历";
            info.Phone = tbPhne.Text;
            bool bRet = SftMgr.DbUpdateRecord(info);
            if (bRet)
            {
                Msg("修改成功");
                CloseFrm();
            }
            else
            {
                Msg("修改失败");
            }
            ModifyName = tbName.Text;
        }
        private delegate void dl_CloseFrm();
        private void CloseFrm()
        {
            if (this.InvokeRequired)
            {
                dl_CloseFrm method = CloseFrm;
                Invoke(method);
                return;
            }
            this.Close();
        }
        private bool ValidateCheck()
        {
            if (!DbMgr.DatabaseFileExsit())
            {
                MessageBox.Show("先联网初始化数据库", "提醒", MessageBoxButtons.OK);
                return false;
            }
            if (tbName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写姓名", "提醒", MessageBoxButtons.OK);
                return false;
            }
            if ((tbPhne.Text != "") && (!Funcs.IsPhoneNum(tbPhne.Text)))
            {
                MessageBox.Show("请正确填写手机号码", "提醒", MessageBoxButtons.OK);
                return false;
            }
            return true;
        }

    }
}
